package com.demo.demo.images;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.net.URLConnection;

/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2023/5/23
 */
public class MergeImages4 {
    public static void main(String[] args) throws Exception {
        // Load the original image file

        URL url = new URL("https://sh-staging-changyi-wecom20-01.oss-cn-shanghai.aliyuncs.com/2023/05/17/4a81f8a8-4c0a-453c-9fe5-7fbc1e0a149e.png");
        URLConnection urlConnection = url.openConnection();
        urlConnection.setConnectTimeout(3000); // 设置连接超时时间为2秒
        urlConnection.setReadTimeout(3000); // 设置读取数据超时时间为2秒
        BufferedImage image = ImageIO.read(urlConnection.getInputStream());
        // Get image dimensions
        int width = image.getWidth();
        int height = image.getHeight();

        // Create graphics object
        Graphics g = image.createGraphics();

        // Draw the image
        g.drawImage(image, 0, 0, null);

        // Add label and price
        String label = "SALE";
        String price = "$19.99";
        Font font = new Font("Arial", Font.BOLD, 24);
        g.setFont(font);
        FontMetrics metrics = g.getFontMetrics();
        int labelWidth = metrics.stringWidth(label);
        int priceWidth = metrics.stringWidth(price);
        g.setColor(Color.red);
        g.drawString(label, width - labelWidth - 10, 30);
        g.drawString(price, width - priceWidth - 10, 60);

        // Save the modified image

        File outputFile = new File("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\output11.png");
        ImageIO.write(image, "png", outputFile);
    }
}
